#pragma once
#include <iostream>
#include <poll.h>
#include "Socket.hpp"
using namespace std;
static const uint16_t defaultport = 8888;
static const int fd_num_max = 64;
int defaultfd = -1;
int non_event = 0;
class PollServer
{
public:
    PollServer(uint16_t port = defaultport) : port_(port)
    {
        for (int i = 0; i < fd_num_max; ++i)
        {
            event_fds_[i].fd = defaultfd;
            event_fds_[i].events = non_event;
            event_fds_[i].revents = non_event;
        }
    }

    bool Init()
    {
        listensock_.Socket();
        listensock_.Bind(port_);
        listensock_.Listen();
        // cout << "listen success..." << endl;
        return true;
    }
    void Accepter()
    {
        std::string clientip;
        uint16_t clientport = 0;
        int sockfd = listensock_.Accept(&clientip, &clientport);
        if (sockfd < 0)
            return;
        // 将这个sockfd添加到数组中
        int pos = 1;
        for (; pos < fd_num_max; ++pos)
        {
            if (event_fds_[pos].fd != defaultfd)
            {
                continue;
            }
            else
            {
                break;
            }
        }
        if (pos == fd_num_max)
        {
            std::cout << "server is full close :" << sockfd << std::endl;
            close(sockfd);
        }
        else
        {
            event_fds_[pos].fd = sockfd;
            event_fds_[pos].events = POLLIN;
        }
    }
    void Recver(int fd, int pos)
    {
        char buffer[1024];
        ssize_t n = read(fd, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << "get a message:" << buffer << std::endl;
        }
        else if (n == 0)
        {
            std::cout << "client quit,me too,close fd is:" << fd << std::endl;
            close(fd);
            event_fds_[pos].fd = defaultfd;
        }
        else
        {
            cout << "recv error" << endl;
            close(fd);
            event_fds_[pos].fd = defaultfd;
        }
    }
    void Dispatcher()
    {
        for (int i = 0; i < fd_num_max; ++i)
        {
            int fd = event_fds_[i].fd;
            if (fd == defaultfd)
            {
                continue;
            }
            if (event_fds_[i].revents & POLLIN)
            {
                if (fd == listensock_.Sockfd())
                {
                    Accepter();
                }
                else
                {
                    Recver(fd, i);
                }
            }
        }
    }
    void Start()
    {
        event_fds_[0].fd = listensock_.Sockfd();
        event_fds_[0].events = POLLIN;
        int timeout = 3000;
        for (;;)
        {
            int n = poll(event_fds_, fd_num_max, timeout);
            switch (n)
            {
            case 0:
                std::cout << "time out..." << std::endl;
                break;
            case -1:
                std::cerr << "select error" << std::endl;
                break;
            default:
                // 有事件就绪了
                std::cout << "get a new link!" << std::endl;
                Dispatcher();
                break;
            }
        }
    }
    ~PollServer()
    {
        close(listensock_.Sockfd());
    }

private:
    Sock listensock_;
    uint16_t port_;
    struct pollfd event_fds_[fd_num_max];
};